package july.android.cakes.service;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import july.android.cakes.service.*;

@Service
@Transactional
public class SService {
    
    @Autowired
    private JdbcTemplate jdbc;
   
    //修改学生头像
    public void UPStuIcon(String id,String icon,String random) throws Exception{
        String oldIcon=getstuimage(id);
        oldIcon=oldIcon.substring(oldIcon.length()-4-15-id.length());
        String filename=id+random+icon.substring(icon.length()-4);
        oldIcon=icon.substring(0,icon.length()-4-15-id.length())+oldIcon;
        System.out.println(oldIcon);
        File del=new File(oldIcon);
        if (del.exists()){
            //System.out.println(del.delete());
            del.delete();
        } 
        filename = "http:" + File.separator + File.separator + "123.206.198.158:8080" + File.separator + "img" + File.separator + "student" + File.separator + filename;
        this.jdbc.update("update student set icon=? where id=?",filename,id);
    }
    
     //修改教师头像
    public void UPTeaIcon(String id,String icon,String random) throws Exception{
        String oldIcon=getteacherimage(id);
        oldIcon=oldIcon.substring(oldIcon.length()-4-15-id.length());
        String filename=id+random+icon.substring(icon.length()-4);
        oldIcon=icon.substring(0,icon.length()-4-15-id.length())+oldIcon;
        System.out.println(oldIcon);
        File del=new File(oldIcon);
        if (del.exists()){
            //System.out.println(del.delete());
            del.delete();
        } 
        filename = "http:" + File.separator + File.separator + "123.206.198.158:8080" + File.separator + "img" + File.separator + "teacher" + File.separator + filename;
        this.jdbc.update("update teacher set icon=? where id=?",filename,id);
    }
    
    //修改社团头像
    public void UPTeamIcon(String id,String icon,String random) throws Exception{
        String oldIcon=getdepimage(id);
        oldIcon=oldIcon.substring(oldIcon.length()-4-15-id.length());
        String filename=id+random+icon.substring(icon.length()-4);
        oldIcon=icon.substring(0,icon.length()-4-15-id.length())+oldIcon;
        System.out.println(oldIcon);
        File del=new File(oldIcon);
        if (del.exists()){
            //System.out.println(del.delete());
            del.delete();
        } 
        filename = "http:" + File.separator + File.separator + "123.206.198.158:8080" + File.separator + "img" + File.separator + "team" + File.separator + filename;
        this.jdbc.update("update team set icon=? where id=?",filename,Integer.parseInt(id));
    }
    
        //根据传入的社团账号获取社团的头像，返回路径 
    public String getdepimage(String id)throws Exception{
       return (String)(this.jdbc.queryForMap("select icon from team where id=?",id)).get("icon");
    }
    
     //根据传入的学生账号判断是什么专业
    public String getmajor(String id){
        return (String)(this.jdbc.queryForMap("select major.nname from student,major where student.ma_id=major.id and student.id=?",id)).get("nname");
    }
     //修改昵称
     public void modifynickname(String id,String nickname){
         this.jdbc.update("update student set nickname=? where id=?",nickname,id);
     }
     
     //根据传入的学生账号以及头像的路径设置学生的新头像
     public void addstuimage(String id,String icon){
         this.jdbc.update("update student set icon=? where id=?",icon,id);
    }

     //根据传入的学生账号获取昵称
     public String getNickname(String id){
         return (String)(this.jdbc.queryForMap("select nickname from student where id=?",id)).get("nickname");
     }
     
    //根据传入的学生账号获取头像，返回路径 
    public String getstuimage(String id)throws Exception{
         return (String)(this.jdbc.queryForMap("select icon from student where id=?",id)).get("icon");
   }   

    //根据传入的教师工号以及头像的路径设置教师的新头像
    public void addteacherimage(String id,String icon){
         this.jdbc.update("update teacher set icon=? where id=?",icon,id);
    }

    ///根据传入的教师工号获取头像，返回路径
    public String getteacherimage(String id)throws Exception{
         return (String)(this.jdbc.queryForMap("select icon from teacher where id=?",id)).get("icon");
   } 
    
    //根据传入的教师工号获取名字
    public String getteacherName(String id)throws Exception{
         return (String)(this.jdbc.queryForMap("select nname from teacher where id=?",id)).get("nname");
   } 

   //根据传入的教师工号获取教师详细信息
    public List<Map<String,Object>>getteacherdetail(String id){
        return this.jdbc.queryForList("select nname,icon,tel,email,address from teacher,tea_contacts where teacher.id=tea_contacts.tea_id and teacher.id=?",id);
    }

   //修改教师联系电话
    public void changeteacherphone(String id,String tel){
        this.jdbc.update("update teacher_contacts set tel=? where tea_id=?",tel,id);
    }

   //修改教师邮箱地址
    public void changeteacheremail(String id,String email){
        this.jdbc.update("update teacher_contacts set email=? where tea_id=?",email,id);
    }
    
     //修改教师办公室地址
    public void changeteacheraddress(String id,String address){
        this.jdbc.update("update teacher_contacts set address=? where tea_id=?",address,id);
    }
    
    //学生登录验证
    public String studentEnter(String id,String passw){
        List<Map<String, Object>> m =this.jdbc.queryForList("select * from stu_login");
        for (int i=0;i<m.size();i++){
            if (m.get(i).get("stu_id").equals(id)){
                if ((this.jdbc.queryForMap("select * from stu_login where stu_id = ?", id)).get("passw").equals(passw)){
                    return "true";
                }
            }
        }
        return "false";
     }

     //教师登录验证
    public String teacherEnter(String id,String passw){
        List<Map<String, Object>> m =this.jdbc.queryForList("select * from tea_login");
        for (int i=0;i<m.size();i++){
            if (m.get(i).get("tea_id").equals(id)){
                if ((this.jdbc.queryForMap("select * from tea_login where tea_id = ?", id)).get("passw").equals(passw)){
                    return "true";
                }
            }
        }
        return "false";
     }

     //社团登录验证
    public String teamEnter(String id,String passw){
        List<Map<String, Object>> m =this.jdbc.queryForList("select * from team_login");
        for (int i=0;i<m.size();i++){
            if (m.get(i).get("team_id").toString().equals(id)){
                if ((this.jdbc.queryForMap("select * from team_login where team_id = ?", id)).get("passw").equals(passw)){
                    return "true";
                }
            }
        }
        return "false";
     }
}
